package com.pj.point.home;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.SeekBar;

import androidx.annotation.NonNull;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;

import com.google.android.material.floatingactionbutton.FloatingActionButton;
import com.google.android.material.snackbar.Snackbar;
import com.pj.point.R;
import com.pj.point.view.PaintView;

public class PaintActivity extends AppCompatActivity implements View.OnClickListener {

    private PaintView paintView;
    private FloatingActionButton red;
    private FloatingActionButton blue;
    private FloatingActionButton orange;
    private FloatingActionButton green;
    private FloatingActionButton black;
    private FloatingActionButton paint;
    private int index = 10;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_paint);
        ActionBar bar = getSupportActionBar();
        bar.setDisplayHomeAsUpEnabled(true);
        bar.setTitle("奇思妙想");
        initView();
        red.setOnClickListener(this);
        blue.setOnClickListener(this);
        orange.setOnClickListener(this);
        green.setOnClickListener(this);
        black.setOnClickListener(this);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.sure,menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        if (item.getItemId() == R.id.sure_menu){
            String path = paintView.saveByUri();
            Intent intent = new Intent();
            intent.putExtra("path",path);
            setResult(-1,intent);
            finish();
        }
        finish();
        return super.onOptionsItemSelected(item);
    }

    private void initView() {
        paintView = (PaintView) findViewById(R.id.paint_view);
        red = (FloatingActionButton) findViewById(R.id.red);
        blue = (FloatingActionButton) findViewById(R.id.blue);
        orange = (FloatingActionButton) findViewById(R.id.orange);
        green = (FloatingActionButton) findViewById(R.id.green);
        black = (FloatingActionButton) findViewById(R.id.black);
        paint = (FloatingActionButton) findViewById(R.id.paint);
        paint.setOnClickListener(view -> {
            paintView.setMode(PaintView.Mode.DRAW);
            AlertDialog.Builder builder = new AlertDialog.Builder(PaintActivity.this);
            SeekBar bar = new SeekBar(this);
            bar.setMax(100);
            bar.setProgress(index);
            builder.setTitle("设置画笔大小")
                    .setView(bar)
                    .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialogInterface, int i) {
                            dialogInterface.dismiss();
                        }
                    });
            AlertDialog dialog = builder.show();
            bar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
                @Override
                public void onProgressChanged(SeekBar seekBar, int i, boolean b) {

                }

                @Override
                public void onStartTrackingTouch(SeekBar seekBar) {

                }

                @Override
                public void onStopTrackingTouch(SeekBar seekBar) {
                    index = seekBar.getProgress();
                    paintView.paint.setStrokeWidth(index);
                    dialog.dismiss();
                }
            });

        });


    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.red: {
                paintView.paint.setColor(getResources().getColor(android.R.color.holo_red_light, getTheme()));
                break;
            }
            case R.id.blue: {
                paintView.paint.setColor(getResources().getColor(android.R.color.holo_blue_light, getTheme()));
                break;
            }
            case R.id.orange: {
                paintView.paint.setColor(getResources().getColor(android.R.color.holo_orange_light, getTheme()));
                break;
            }
            case R.id.green: {
                paintView.paint.setColor(getResources().getColor(R.color.teal_700, getTheme()));
                break;
            }
            case R.id.black: {
                paintView.paint.setColor(getResources().getColor(R.color.black, getTheme()));
                break;
            }
        }
    }
}